#pragma once

#include "iconfiguration_provider.h"
#include <QString>
#include <QStringList>
#include <QRegularExpression>

namespace infrastructure {

/**
 * @class EnvironmentVariableProvider
 * @brief 从环境变量提供配置的提供者
 */
class EnvironmentVariableProvider : public IConfigurationProvider {
public:
    explicit EnvironmentVariableProvider(int priority = 200);
    explicit EnvironmentVariableProvider(const QString& prefix, int priority = 200);
    explicit EnvironmentVariableProvider(const QStringList& allowedKeys, int priority = 200);
    virtual ~EnvironmentVariableProvider() = default;

    // IConfigurationProvider interface
    ConfigurationData load() override;
    bool isAvailable() const override;
    QString getDescription() const override;
    void setChangeCallback(ConfigurationChangeCallback callback) override;
    void startWatching() override;
    void stopWatching() override;
    int getPriority() const override;

    // EnvironmentVariableProvider 特有的方法
    void setPrefix(const QString& prefix) { m_prefix = prefix; }
    QString getPrefix() const { return m_prefix; }
    
    void setAllowedKeys(const QStringList& keys) { m_allowedKeys = keys; }
    QStringList getAllowedKeys() const { return m_allowedKeys; }
    
    void setKeyTransformation(bool enabled) { m_transformKeys = enabled; }
    bool isKeyTransformationEnabled() const { return m_transformKeys; }

private:
    QString m_prefix;           // 环境变量前缀过滤器
    QStringList m_allowedKeys;  // 允许的键列表
    int m_priority;
    bool m_transformKeys;       // 是否转换键名格式（下划线转冒号等）
    ConfigurationChangeCallback m_callback;
    
    QString transformKey(const QString& envKey) const;
    bool isKeyAllowed(const QString& envKey) const;
    QString getEnvironmentValue(const QString& key) const;
};

} // namespace infrastructure 